package com.kdgc.power.gen;


import com.kdgc.power.common.util.StringUtil;
import org.beetl.core.Template;
import org.beetl.sql.clazz.kit.StringKit;
import org.beetl.sql.gen.BaseProject;
import org.beetl.sql.gen.Entity;
import org.beetl.sql.gen.SourceConfig;

import java.io.Writer;
import java.util.Arrays;

public class ControllerSourceBuilder extends TemplateSourceBuilder {
    public static String controllerPath = "controller.btl";
    String suffix = "Controller";

    public ControllerSourceBuilder() {
        super("controller");

    }


    public void generate(BaseProject project, SourceConfig config, Entity entity) {
        Template template = groupTemplate.getTemplate(controllerPath);
        String entityClass = entity.getName();
        template.binding("className", entityClass);
        template.binding("package", project.getBasePackage(this.name));

        String entityPkg = project.getBasePackage("entity");
        String entityHead =  entityPkg + "." +entityClass;
        String servicePkg = project.getBasePackage("service");
        String serviceHead =  servicePkg + "." +entityClass + "Service";
        template.binding("imports", Arrays.asList(entityHead,serviceHead));
        template.binding("smallClass", StringKit.toLowerCaseFirstOne(entityClass));
        Writer writer = project.getWriterByName(this.name, entity.getName() + this.suffix + ".java");
        template.renderTo(writer);
    }
}
